<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this -> display();
    }

    //captcha方法
    public function captcha(){
        //配置
        $cfg = array(
            'fontSize'  =>  14,              // 验证码字体大小(px)
            'useCurve'  =>  false,            // 是否画混淆曲线
            'useNoise'  =>  false,            // 是否添加杂点	
            //'imageH'    =>  39,
            //'imageW'    =>  90,
            'length'    =>  3,               // 验证码位数
            'fontttf'   =>  '4.ttf',              // 验证码字体，不设置随机获取
            'codeSet'   =>  '123456789',              //验证码字符集合
        );
        //实例化验证码类
        $verify = new \Think\Verify($cfg);
        //输出验证码
        $verify -> entry();
    }

    //检索数据
    public function result(){
        //每页记录数
        $number_per_page = 8;
        if(IS_POST){
            //提交数据
            $post = I('post.');
            //dump($post);die;
            if($post['yw2'] === ''){
                $this -> error('请填写印文！');
            }
            
            $where = "yw2 like '%" . $post['yw2'] . "%'";
            if($post['shidai'] !== ''){
                $where .= " and shidai like '%" . $post['shidai'] . "%'";
            }
            if($post['zhulu'] !== ''){
                $where .= " and zhulu like '%" . $post['zhulu'] . "%'";
            }
            //dump($where);die;
            //$data = M('Data') -> where($where) -> select();
            //dump($data);die;
            $model = M('Data');
            //分页第一步：查询总的记录数
            $count = $model -> where($where) -> count();
            //分页第二步：实例化分页类，传递参数
            $page = new \Think\Page($count,$number_per_page);  //每页显示1个
            //分页第三步：可选步骤，定制分页按钮的提示文字
            $page -> rollPage = 1;
            $page -> lastSuffix = false;
            $page -> setConfig('prev','上一页');
            $page -> setconfig('next','下一页');
            $page -> setconfig('last','末页');
            $page -> setconfig('first','首页');
            $page -> setconfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE%/%TOTAL_PAGE% %DOWN_PAGE% %END%");
            //分页第四步：通过show方法输出分页的URL链接
            $show = $page -> show();
            $show = bootstrap_page_style2($show);
            //dump($show);die;
            //分页第五步：使用limit方法查询数据
            //$data = $model -> where($where) -> limit($page -> firstRow,$page -> listRows) -> select();
            $data = M('Data') -> field('t1.*,t2.folder as folder')
                    -> alias('t1')
                    -> join('left join yh_category as t2 on t1.category_id = t2.id')
                    -> where($where)
                    -> limit($page -> firstRow,$page -> listRows)
                    -> select();
            //dump($data);die;
            //分页第六步：传递给模板
            //把分页条件放在session里，供以后判断查询
            session('where',$where);

            //计算页数向下取整
            $page_count = floor($count/$number_per_page);
            //dump($page_count);die;
            $this -> assign('page_count',$page_count);
            //查询记录数
            $this -> assign('count',$count);
            $this -> assign('data',$data);
            $this -> assign('show',$show);
            //分页第七步：展示模板
            $this -> display();
        }else{
            //get请求
            //判断用户是否有where
            $where = session('where');
            if(empty($where)){
               $this -> error('参数有误，请重新查询',U('index'));
            }

            $model = M('Data');
            //分页第一步：查询总的记录数
            $count = $model -> where($where) -> count();
            //分页第二步：实例化分页类，传递参数
            $page = new \Think\Page($count,$number_per_page);  //每页显示1个
            //分页第三步：可选步骤，定制分页按钮的提示文字
            $page -> rollPage = 1;
            $page -> lastSuffix = false;
            $page -> setConfig('prev','上一页');
            $page -> setconfig('next','下一页');
            $page -> setconfig('last','末页');
            $page -> setconfig('first','首页');
            $page -> setconfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE%/%TOTAL_PAGE% %DOWN_PAGE% %END%");
            //分页第四步：通过show方法输出分页的URL链接
            $show = $page -> show();
            $show = bootstrap_page_style2($show);
            //dump($show);die;
            //分页第五步：使用limit方法查询数据
            //$data = $model -> where($where) -> limit($page -> firstRow,$page -> listRows) -> select();
            $data = M('Data') -> field('t1.*,t2.folder as folder')
                        -> alias('t1')
                        -> join('left join yh_category as t2 on t1.category_id = t2.id')
                        -> where($where)
                        -> limit($page -> firstRow,$page -> listRows)
                        -> select();
            //dump($data);die;
            //分页第六步：传递给模板
            //计算页数向下取整
            $page_count = floor($count/$number_per_page);
            //dump($page_count);die;
            $this -> assign('page_count',$page_count);
            //查询记录数
            $this -> assign('count',$count);
            $this -> assign('data',$data);
            $this -> assign('show',$show);
            //分页第七步：展示模板
            $this -> display();
        }
    }

    //详细页
    public function details(){
        //dump($_SERVER["HTTP_REFERER"]);die;
        //防止直接访问详细页
        $referer = $_SERVER["HTTP_REFERER"];
        if(!strstr($referer,"result")){
            $this -> error('非法访问！',U('index'));
        }
        //接收id
        $id = I('get.id');
        //sql=select t1.*,t2.folder as folder from yh_data as t1 left join yh_category as t2 on t1.category_id = t2.id where t1.id = get.id
        //获取数据
        $data = M('Data') -> field('t1.*,t2.folder as folder')
                        -> alias('t1')
                        -> join('left join yh_category as t2 on t1.category_id = t2.id')
                        -> where('t1.id = ' . $id)
                        -> find();
        //dump($data);die;
        //图片路径拼接一下
        $data['kid'] = '/Uploads/' . $data['folder'] . '/' . $data['kid'];
        //判断边款
        //dump(is_null($data['biankuan1']));die;
        if(!is_null($data['biankuan1'])){
            $data['biankuan1'] = '/Uploads/' . $data['folder'] . '/' . $data['biankuan1'];
        }
        if(!is_null($data['biankuan2'])){
            $data['biankuan2'] = '/Uploads/' . $data['folder'] . '/' . $data['biankuan2'];
        }
        if(!is_null($data['biankuan3'])){
            $data['biankuan3'] = '/Uploads/' . $data['folder'] . '/' . $data['biankuan3'];
        }
        if(!is_null($data['biankuan4'])){
            $data['biankuan4'] = '/Uploads/' . $data['folder'] . '/' . $data['biankuan4'];
        }
        //dump($data);die;
        $this -> assign('data',$data);
        //展示模板
        $this -> display();
    }
}